﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using Dycox.ComponentModel;
using Dycox.Validation;
using Newtonsoft.Json;

namespace Dycox.Workflows
{
    [DataContract]
    public class StageOperationContract  : OperationContract
    {

        public StageOperationContract()
        {

        }

        //public virtual void Apply(OperationArguments args, WFStep step, Workflow workflow, bool modifying)
        //{
        //}

        //protected void CopyProperties(object target, params ModelPropertyUsage[] usages)
        //{
        //    if (target != null)
        //    {
        //        PropertyDescriptorCollection pdct = TypeDescriptor.GetProperties(target);

        //        foreach (ModelPropertyUsage u in usages)
        //        {
        //            PropertyDescriptorCollection pdcs = TypeDescriptor.GetProperties(this, new Attribute[] { ModelPropertyAttribute.GetInstance(u) });
        //            foreach (PropertyDescriptor pds in pdcs)
        //            {
        //                PropertyDescriptor pdt = pdct[pds.Name];
        //                if (pdt != null)
        //                    pdt.SetValue(target, pds.GetValue(this));
        //            }

        //        }
        //    }
        //}

        //public virtual void Undo(WorkflowUndoContext context)
        //{
        //    context.Restore();
        //}

        internal protected virtual void InitializeNew(ContractInitArguments args)
        {
            var info = args.Stage.ContractInfo;
            var pcoll = info.PropertyValues;

            if (info != null && pcoll.Count > 0)
            {
                PropertyDescriptorCollection coll = TypeDescriptor.GetProperties(this);

                foreach (string name in pcoll.AllKeys)
                {
                    PropertyDescriptor prop = coll[name];

                    if (prop != null)
                    {
                        object value;

                        string text = pcoll[name];
                        if (text == null) value = null;
                        else value = Dycox.Utils.ConvertUtil.ConvertType(text, new SimpleTypeDescriptorContext(this, prop));

                        prop.SetValue(this, value);
                    }
                    else
                        throw new WorkflowException(SR.GetString("property_not_found", prop.Name, this.GetType().FullName));
                }
            }
        }

        private string _userAction;

        [
        DataMember(EmitDefaultValue = false),
        DefaultValue(null),
        ModelProperty(ModelPropertyUsage.Control, false),
        Browsable(false)
        ]
        public string UserAction
        {
            get { return _userAction; }
            set
            {
                if (_userAction != value)
                {
                    _userAction = value;
                    SendPropertyChanged("UserAction");
                }

            }
        }

        public override bool IsOperatorNameRequired
        {
            get
            {
                return true;
            }
        }
    }
}
